package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.events;
import com.example.springboot.entity.interviews;
import com.example.springboot.entity.user;
import com.example.springboot.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:7070/")
@RestController
@RequestMapping("/events")
public class EventController {
    @Autowired
    private EventService eventService;

    @GetMapping("/all")
    public Result getAllEvents() {
        try {
            List<events> events = eventService.findAllEvents();
            return Result.success(events);
        } catch (Exception e) {
            return Result.error("500", "获取活动列表失败");
        }
    }


    @GetMapping("/{id}")
    public ResponseEntity<events> getEventById(@PathVariable Long id) {
        events event = eventService.findEventById(id);
        return ResponseEntity.ok().body(event);
    }

    @GetMapping("/all/{id}")
    public Result getAllEventsByid(@PathVariable Long id) {
      List<events> eventsList= eventService.getAllEventsByid(id);
        return Result.success(eventsList);
    }

    @PostMapping("/create")
    public Result addevent(@RequestBody events event) {
        eventService.addevent(event);
        return Result.success("新增成功");
    }
//
//    @PostMapping("/create")
//    public ResponseEntity<events> createEvent(@RequestBody events event) {
//        eventService.saveEvent(event);
//        return ResponseEntity.ok().body(event);
//    }

    @PutMapping("/update/{id}")
    public ResponseEntity<events> updateEvent(@PathVariable Long id, @RequestBody events event) {
        event.setEvent_id(id); // Ensure the ID in the path matches the ID in the request body
        eventService.saveEvent(event);
        return ResponseEntity.ok().body(event);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Void> deleteEvent(@PathVariable Long id) {
        eventService.deleteEvent(id);
        return ResponseEntity.ok().build();
    }
}
